package com.lhdx.practical.utils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;

/**
 * 分页器
 */
public class PageVo<T> {
    /**
     * 页码
     */
    private int pageNum;
    /**
     * 页码总数
     */
    private int pageCount;
    /**
     * 总数
     */
    private int count;

    /**
     * 数量
     */
    private int rows;
    /**
     * 数据
     */
    private List<T> list;
    /**
     * 页码HTML
     */
    private String pageNumHtml;
    /**
     * 前台页码html
     */
    private String webPageHtml;
    /**
     * 参数
     */
    private Map<String, String> args = new HashMap<String, String>();

    public PageVo(int pageNum,Map<String, String> args, Page<T> pageResult) {
        this.pageNum = pageNum;
        this.count = (int)pageResult.getTotalElements();
        this.pageCount = pageResult.getTotalPages();
        this.list = pageResult.getContent();
        this.args = args;
    }

    public int getPageNum() {
        if (this.pageNum <= 0) {
            this.pageNum = 0;
            return 0;
        } else {
            return pageNum;
        }
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageCount() {
        //this.pageCount = ((this.getCount() - 1) / this.getRows()) + 1;
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public String getUrl(int num) {
        Iterator<Entry<String, String>> iter = this.getArgs().entrySet().iterator();
        List<String> values = new ArrayList<String>();
        while (iter.hasNext()) {
            Map.Entry<String, String> entry = iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            values.add(key + "=" + val);
        }
        num = num < 0 ? 0 : num;
        values.add("p=" + num);
        return "?" + StringUtils.join(values.toArray(), "&");
    }

    public void setPageNumHtml(String pageNumHtml) {
        this.pageNumHtml = pageNumHtml;
    }

    public String getPageNumHtml() {
        StringBuffer sb = new StringBuffer();
        sb.append("<ul class=\"pagination\">");
        // 首页，上一页
        if (this.getPageNum() != 0) {
            sb.append("<li><a href='" + this.getUrl(0) + "' title='首页'>&lt;&lt;</a></li>");
            sb.append("<li><a href='" + this.getUrl(this.getPageNum() - 1) + "' title='上一页'>&lt;</a></li>");
        }
        // 页码
        if (this.getPageCount() != 1) {
            int startNum = this.getPageNum() - 3 <= 1 ? 1 : this.getPageNum() - 3;
            int endNum = this.getPageNum() + 3 >= this.getPageCount() ? this.getPageCount() : this.getPageNum() + 3;
            if (startNum > 1) {
                sb.append("<li><a href='javascript:void(0);'>...</a></li>");
            }
            for (int i = startNum; i <= endNum; i++) {
                if ((i-1) == pageNum) {
                    sb.append("<li class='active'><a href='" + this.getUrl(i-1) + "' class='number current' title='" + i + "'>" + i + "</a></li>");
                } else {
                    sb.append("<li><a href='" + this.getUrl(i-1) + "' class='number' title='" + i + "'>" + i + "</a></li>");
                }
            }
            if (endNum < this.getPageCount()) {
                sb.append("<li><a href='javascript:void(0);'>...</a></li>");
            }
        }
        // 下一页，尾页
        if (this.getPageNum() < this.getPageCount()) {
            sb.append("<li><a href='" + this.getUrl(this.getPageNum()) + "' title='下一页'>&gt;</a></li>");
            sb.append("<li><a href='" + this.getUrl(this.getPageCount()-1) + "' title='末页'>&gt;&gt;</a></li>");
        }
        sb.append("</ul>");
        return sb.toString();
    }

    public String getPageHtml() {
        StringBuffer sb = new StringBuffer();
        // 首页，上一页
        if (this.getPageNum() != 1) {
            sb.append("<div class='page prve'>");
            sb.append("<a href='" + this.getUrl(this.getPageNum() - 1) + "' title='上一页'><i class=\"icon-fanhui1 iconfont fan\"></i></a>");
            sb.append("</div>");
        }
        // 页码
        if (this.getPageCount() != 1) {
            int startNum = this.getPageNum() - 3 <= 1 ? 1 : this.getPageNum() - 3;
            int endNum = this.getPageNum() + 3 >= this.getPageCount() ? this.getPageCount() : this.getPageNum() + 3;
            if (startNum > 1) {
                sb.append("<div class='page pagenumber'>...</div>");
            }
            for (int i = startNum; i <= endNum; i++) {
                if (i == pageNum) {
                    sb.append("<a href='" + this.getUrl(i) + "' title='" + i + "'><div class='page pageactive pagenumber' id='active' data-number='1'>" + i + "</div></a>");
                } else {
                    sb.append("<a href='" + this.getUrl(i) + "' title='" + i + "'><div class='page pagenumber'>" + i + "</div></a>");
                }
            }
            if (endNum < this.getPageCount()) {
                sb.append("<div class='page pagenumber'>...</div>");
            }
        }

        // 下一页，尾页
        if (this.getPageNum() < this.getPageCount()) {
            sb.append("<div class=\"page next\">");
            sb.append("<a href='" + this.getUrl(this.getPageNum() + 1) + "' title='下一页'><i class=\"icon-youjiantou2 iconfont\"></i></a>");
            sb.append("</div>");
        }

        return sb.toString();
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Map<String, String> getArgs() {
        return args;
    }

    public void setArgs(Map<String, String> args) {
        this.args = args;
    }

    public String getWebPageHtml() {
        StringBuffer sb = new StringBuffer();
        sb.append("<li class=\"prev\"><a href=\""+this.getUrl(1)+"\">上一页</a></li>");
        if (this.getPageCount() != 1) {
            int startNum = this.getPageNum() - 3 <= 1 ? 1 : this.getPageNum() - 3;
            int endNum = this.getPageNum() + 3 >= this.getPageCount() ? this.getPageCount() : this.getPageNum() + 3;
            for (int i = startNum; i <= endNum; i++) {
                if (i == pageNum) {
                    sb.append("<li class=\"selected\"><a href=\""+this.getUrl(i)+"\">"+i+"</a></li>");
                } else {
                    sb.append("<li><a href=\""+this.getUrl(i)+"\">"+i+"</a></li>");
                }
            }
        }
        int endpage = this.getPageNum() + 1 > this.getPageCount() ? this.getPageCount() : this.getPageNum() + 1;
        sb.append("<li class=\"next\"><a href=\""+this.getUrl(endpage)+"\">下一页</a></li>");
        return sb.toString();
    }

    public void setWebPageHtml(String webPageHtml) {
        this.webPageHtml = webPageHtml;
    }
}
